package com.ssm.zy.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.zy.common.controller.BaseController;
import com.ssm.zy.common.result.Result;
import com.ssm.zy.common.result.ResultCode;
import com.ssm.zy.pojo.City;
import com.ssm.zy.pojo.User;
import com.ssm.zy.pojo.vo.UserVO;
import com.ssm.zy.pojo.vo.UserVos;
import com.ssm.zy.service.UserService;
import com.ssm.zy.utils.JWTUtil;
import com.ssm.zy.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user")//用户
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private JWTUtil jwtUtil;

    @GetMapping("checkUser")//查找个人信息
    public Result<User> checkUser(){
        Integer userId=super.userId;
        User user =  userService.getcheckUser(userId);
        if (user==null){
            return Result.err(ResultCode.ERROR);
        }else {
            return new Result(ResultCode.SUCCESS,user);
        }

    }

    //登录
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
        String jwt = userService.createJWTByUserInfo(user);
        if (StringUtils.isEmpty(jwt)){
            return Result.err(ResultCode.LOGIN_ERROR);
        }
        return new Result<String>(ResultCode.SUCCESS,jwt);
    }

    //获取用户详情信息
    @GetMapping("/getInfo")
    public Result<UserVO> getUserInfo(){
        UserVO userDetailInfo = userService.getUserDetailInfo(super.userId);
        return new Result<UserVO>(ResultCode.SUCCESS,userDetailInfo);
    }

    @PostMapping("/register")//注册
    public Result register(@RequestBody User user, Integer sheng,Integer shi,Integer qu){
        int i = userService.register(user,sheng,shi,qu);
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }
    @PutMapping("/updatePassword")//修改密码
    public Result updatePassword(String oldPassword,String newPassword){
        // 旧密码，新密码进行修改密码
        int i = userService.updatePassword(super.userId,oldPassword,newPassword);
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }
    @GetMapping("/checkUsername")//查找用户名是否重复
    public Result checkUsername(String username){
        String userName = userService.checkUsername(username);
        if (userName==null|| userName.trim()==""){
            return Result.ok();
        }else {
            return Result.errUsername();
        }

    }

    @GetMapping("/checkPhone")//查找手机号是否重复
    public Result checkPhone(String phone){
        String userPhone = userService.checkPhone(phone);
        if (userPhone==null|| userPhone.trim()==""){
            return Result.ok();
        }else {
            return Result.errPhone();
        }

    }

    @GetMapping("/checkLikeUser")//模糊用户查询
    public Result checkLikeUser(String likeName,
                                @RequestParam(defaultValue = "1")Integer pageNum,
                                @RequestParam(defaultValue = "5")Integer pageSize){
        PageInfo<User> userList=  userService.getcheckLikeUser(likeName , pageNum , pageSize);
        if (userList.getSize()==0){
            return Result.err();
        }
        return new Result(ResultCode.SUCCESS,userList);
    }

    @GetMapping("/findCityByParentId")//查找城市
    public Result findCityByParentId(Integer parentId){
        List<City> cityList=userService.findCityByParentId(parentId);
        return new Result(ResultCode.SUCCESS,cityList);
    }

    @GetMapping("/findAddress")//解析地址
    public Result findAddress(String address){
        List<Integer> list=userService.findAddress(address);
        return new Result(ResultCode.SUCCESS,list);
    }

    @PostMapping("/updateUser")//修改用户信息
    public Result updateUser(@RequestBody User user,Integer sheng,Integer shi,Integer qu){
        Integer userId=super.userId;
        if (user.getId()==userId){
            int i=userService.updateUser(user,sheng,shi,qu);
            if (i>0){
                return Result.ok();
            }
        }
        return Result.err();
    }
    //删除用户
    @DeleteMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable String userId) {
        int myid=super.userId;
        int i=userService.deleteUser(Integer.parseInt(userId),myid);
        if(i==0){
            return Result.err();
        }
        return Result.ok();
    }

    //提升权限
    @PutMapping("/userLevelUp")
    public Result userLevelUp(Integer userId){
        int i=userService.userLevelUp(userId);
        if (i>0){
            return Result.ok();
        }
        return Result.err();
    }

    @PostMapping("/fileUpload")
    public Result<String> getFileUploadPath(@RequestParam("file") MultipartFile file) throws IOException {
        String filePath = OSSUtil.storageFile(file);
        return new Result<String>(ResultCode.SUCCESS, filePath);
    }

    @GetMapping("/toCheckUser")//查找此用户详情
    public Result<UserVos> toCheckUser(Integer userId){
        Integer myid=super.userId;
        UserVos user =  userService.toCheckUser(userId,myid);
        if (user==null){
            return Result.err(ResultCode.ERROR);
        }else {
            return new Result(ResultCode.SUCCESS,user);
        }

    }
}
